home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / misc / unix / adt-passive.lha / adt-passive.patch next >
Encoding:
Text File  |  2002-05-29  |  3.1 KB  |  119 lines

  1. --- adt.c    Thu Feb 15 02:20:45 2001
  2. +++ adt-passive.c    Sun May 26 01:50:48 2002
  3. @@ -171,9 +171,9 @@
  4.  char *FindDesc = "Performing local case insensitive substring search";
  5.  char GotComplete, GotLocal, GotRecent, FilesAreRemote;
  6.  char Flat, Readme, Silent, Path[250], Action;
  7.  char InitialSetup;
  8. -char Connected;
  9. +char Connected, Passive_FTP;
  10.  char MaskSIGINT, GotSIGINT;
  11.  
  12.  int (*disp_init) ();
  13.  int (*disp_cleanup) ();
  14. @@ -2039,8 +2039,43 @@
  15.  
  16.    return (result >= 100) && (result < 400);
  17.  }
  18.  
  19. +int bftp_getdataconnection_passive(struct sockaddr_in *sin, char *buf) {
  20. +  int d, i, a[7];
  21. +  char *p = buf;
  22. +
  23. +  /* send PASV command */
  24. +  if (!bftp_io) return -1;
  25. +  fflush(bftp_io); rewind(bftp_io);
  26. +  fputs("PASV\r\n", bftp_io);
  27. +  fflush(bftp_io); rewind(bftp_io);
  28. +  if (!fgets(buf, BFTP_BUFSIZE, bftp_io)) return -1;
  29. +
  30. +  /* response should be "227 Entering Passive Mode (A,B,C,D,E,F)\r\n"
  31. +   * where A-F are numbers from 0 to 255. The IP address to connect
  32. +   * to for data is A.B.C.D, and the port is (E<<8 + F)
  33. +   */
  34. +  for (i = 0; i < 7; i++) {
  35. +    d = 0;
  36. +    while (*p && (*p < '0' || *p > '9')) p++;
  37. +    while (*p && (*p >= '0' && *p <= '9')) d = ((d * 10) + (*p-'0')), p++;
  38. +    if (d > 255) return -1;
  39. +    a[i] = d;
  40. +  }
  41. +
  42. +  if (!*p || (a[0] != 227)) return -1;
  43. +
  44. +  sin->sin_family = AF_INET;
  45. +  sin->sin_addr.s_addr = htonl((a[1]<<24) | (a[2]<<16) | (a[3]<<8) | a[4]);
  46. +  sin->sin_port = htons((a[5]<<8) | a[6]);
  47. +
  48. +  i = sizeof(struct sockaddr_in);
  49. +  if ((d = socket(PF_INET, SOCK_STREAM, 0)) < 0) return -1;
  50. +  if (connect(d, (struct sockaddr *) sin, i) < 0) { close(d); return -1; }
  51. +  return d;
  52. +}
  53. +
  54.  int bftp_getdataconnection()
  55.  {
  56.    struct sockaddr_in sin;
  57.    struct hostent *hp;
  58. @@ -2092,13 +2127,22 @@
  59.  
  60.    sprintf(buf, "Getting %s...", remote);
  61.    progress(buf);
  62.  
  63. -  if ((s = bftp_getdataconnection()) >= 0) {
  64. +  s = (Passive_FTP)
  65. +    ? bftp_getdataconnection_passive(&sin, buf)
  66. +    : bftp_getdataconnection();
  67. +  if (s >= 0) {
  68.      sprintf(buf, "RETR %s\n", remote);
  69.      if (bftp_cmd(buf)) {
  70.        i = sizeof(sin);
  71. -      if ((d = accept(s, (struct sockaddr *) & sin, &i)) >= 0) {
  72. +      if (Passive_FTP) {
  73. +    d = s; s = -1;
  74. +      }
  75. +      else {
  76. +    d = accept(s, (struct sockaddr *) & sin, &i);
  77. +      }
  78. +      if (d >= 0) {
  79.      if ((out = fopen(local, "w"))) {
  80.        int len, k, start = time(NULL), t;
  81.        float kps;
  82.  
  83. @@ -2273,8 +2317,9 @@
  84.  
  85.  int bftp_init()
  86.  {
  87.    Connected = 0;
  88. +  Passive_FTP = 0;
  89.    trans_options = bftp_options;
  90.    trans_connect = bftp_connect;
  91.    trans_disconnect = bftp_disconnect;
  92.    trans_remopen = bftp_remopen;
  93. @@ -2283,8 +2328,15 @@
  94.  
  95.    return 0;
  96.  }
  97.  
  98. +int pftp_init()
  99. +{
  100. +  bftp_init();
  101. +  Passive_FTP = 1;
  102. +  return 0;
  103. +}
  104. +
  105.  #endif
  106.  
  107.  /* =================================FIND CLIENT=============================== */
  108.  #ifndef NO_FIND_CLIENT
  109. @@ -4144,8 +4196,9 @@
  110.  TRANSFER TransferTypes[] =
  111.  {
  112.  #ifndef NO_BUILTIN_FTP
  113.    "bftp",   "Builtin FTP: Use internal ftp routines to download files     ", bftp_init,
  114. +  "pftp",   "Passive FTP: passive-mode ftp. useful if you are firewalled  ", pftp_init,
  115.  #endif
  116.  #ifndef NO_EXTERNAL_FTP
  117.    "ftp",   "External FTP: Use the 'ftp' program to download files         ", xftp_init,
  118.  #endif
  119.